﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>
@* <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="//unpkg.com/layui@2.9.20/dist/layui.js"></script> *@
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>IMES</title>
</head>
<body>
    <div class="layui-panel">
        <div style="padding: 32px;">
            <div class="layui-form-item">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">单据编号:</label>
                        <div class="layui-input-block">
                            <input type="text" Id="orderNo" placeholder="请输入，忽略将自动生成" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注:</label>
                        <div class="layui-input-block">
                            <textarea Id="orderNotes" type="text" placeholder="请输入备注" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-panel" style="margin-top: 20px;">
            <div style="padding: 32px;">
                <div class="layui-form-item">
                    <div class="layui-form-label">产品明细</div>
                    <div style="margin-left:800px" class="layui-inline">
                        <a class=" layui-btn-sm" lay-on="test-page-custom">+选择产品</a>
                        <a class=" layui-btn-sm">删除行</a>
                        <a class=" layui-btn-sm">刷新</a>
                    </div>
                </div>

                <table class="layui-table">
                    <thead>
                        <tr>
                            <th>
                                <input type="checkbox" name="check"></input>
                            </th>
                            <th>产品编号</th>
                            <th>产品名称</th>
                            <th>产品规格</th>
                            <th>工单编号</th>
                            <th>数量</th>
                            <th>完成数</th>
                            <th>创建时间</th>
                            <th>创建人</th>
                            <th>修改人</th>
                            <th>修改时间</th>
                        </tr>
                    </thead>
                    <tbody id="body">
                    </tbody>
                </table>
                <div>
                    @* 红色保存按钮 *@
                    <button class="layui-btn layui-btn-danger" lay-submit lay-filter="add">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <script>

        var check = localStorage.getItem("check")
        $(function () {
            console.log(123, check)
            getProductCheck()
        })
        // 获取产品明细
        function getProductCheck() {
            $.ajax({
                url: '@ViewBag.Darius' + "/api/Orders/ProductCheck",
                type: "post",
                data: JSON.stringify({ ProductIds: check }),
                dataType: "json",
                contentType: "application/json",
                success: function (res) {
                    console.log(res)
                    $(res.data).each(function (item) {
                        $("#body").append(`<tr>
                                                       <td>
                                                         <input type="checkbox" name="check"></input>
                                                       </td>
                                                       <td>${this.productNumber}</td>
                                                       <td>${this.productName}</td>
                                                       <td>${this.productSpecification}</td>
                                                       <td>待生成</td>
                                                       <td>
                                                         <input type="text" style="width: 100px" value="1" autocomplete="off" class="layui-input">
                                                       </td>
                                                       <td>0</td>
                                                       <td>${this.createDate}</td>
                                                       <td>${this.createName}</td>
                                                       <td>${this.updateName}</td>
                                                       <td>${this.updateDate}</td>
                                                   </tr>`)
                    })
                }
            })
        }




    </script>

</body>
</html>


<script>
    //弹窗产品选择
    layui.use(function () {
        var $ = layui.$;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;





        // 事件
        util.on('lay-on', {
            'test-page-custom': function () {
                layer.open({
                    type: 2,
                    area: ['800px', '800px'],
                    shadeClose: true,
                    title: '选择产品',
                    content: '/Orders/ProductShowView',

                });
            }
        });
        form.on('submit(add)', function (data) {

            var data = {
                orderNo: $("#orderNo").val(),
                orderNotes: $("#orderNotes").val(),
                createName: "小张",
                orderDetails: []
            }
            //往集合中添加数据

            var trs = $("#body").find("tr")
            $(trs).each(function (item) {
                var td = $(this).find("td")
                if ($(td[0]).find("input[type='checkbox']").is(":checked")) {
                    var productNumber = $(td[1]).text()
                    var productNum = $(td[5]).find("input[type='text']").val()
                    data.orderDetails.push({
                        productNumber: productNumber,
                        productNum: productNum,
                    })
                }
            })
            console.log(data)
            $.ajax({
                url: '@ViewBag.Darius' + "/api/Orders/CreateOrder",
                type: "post",
                async: false,
                data: JSON.stringify(data),
                dataType: "json",
                contentType: "application/json",
                success: function (res) {
                    console.log(res)
                    if (res.code > 0) {
                        //保存成功，添加审批信息
                        function approvalAdd() {
                            var data1={
                                submitName:localStorage.getItem("username"),
                                submitEventId:1,
                                approvalsDto:[]
                            }
                            $.ajax({
                                url: '@ViewBag.Darius' + "/api/Approval/AddApproval",
                                type: "post",
                                async: false,
                                data: JSON.stringify(data1),
                                dataType: "json",
                                contentType: "application/json",
                                success: function (res) {
                                    console.log(res)
                                    if (res.code > 0) {
                                        layer.msg("保存成功,等待审批", function () {
                                            //关闭当前弹出层
                                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                            parent.layer.close(index); //再执行关闭
                                            //刷新父页面
                                            parent.location.reload();
                                        });
                                    } else {
                                        layer.msg(res.messege);
                                    }
                                }
                            })
                        }

                    }
                }
            })

        })



    });

</script>


